package com.example.mapexport.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.AMapUtils;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.amap.api.maps2d.model.PolygonOptions;
import com.amap.api.services.core.LatLonPoint;
import com.example.mapexport.R;
import com.example.mapexport.utils.MapUtils;

import java.util.ArrayList;
import java.util.List;

public class MapActivity extends AppCompatActivity {

    private MapView mapView;
    private AMap aMap;
    private static final int LOCATION_ZOOM_LEVEL = 13;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_map);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        mapView = findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);

        //初始化地图控制器对象
        if (aMap == null) {
            aMap = mapView.getMap();
            LatLng latLng = new LatLng(31.385476,120.980795);

            aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, LOCATION_ZOOM_LEVEL));
            final Marker marker = aMap.addMarker(new MarkerOptions().position(latLng).title("昆山").snippet("DefaultMarker"));

            // 绘制一个长方形
            aMap.addPolygon(new PolygonOptions()
                    .addAll(MapUtils.getInstance().createRectangle(31.385476,120.980795))
                    .fillColor(Color.argb(55, 1, 1, 1)).strokeColor(Color.RED).strokeWidth(1));

            aMap.addPolygon(new PolygonOptions()
                    .addAll(MapUtils.getInstance().pointsCreate4Cutting(31.385476,120.980795).get(0))
                    .fillColor(Color.argb(55, 255, 0, 0)).strokeColor(Color.RED).strokeWidth(1));
            aMap.addPolygon(new PolygonOptions()
                    .addAll(MapUtils.getInstance().pointsCreate4Cutting(31.385476,120.980795).get(1))
                    .fillColor(Color.argb(55, 0, 0, 255)).strokeColor(Color.RED).strokeWidth(1));
            aMap.addPolygon(new PolygonOptions()
                    .addAll(MapUtils.getInstance().pointsCreate4Cutting(31.385476,120.980795).get(2))
                    .fillColor(Color.argb(55, 0, 128, 0)).strokeColor(Color.RED).strokeWidth(1));
            aMap.addPolygon(new PolygonOptions()
                    .addAll(MapUtils.getInstance().pointsCreate4Cutting(31.385476,120.980795).get(3))
                    .fillColor(Color.argb(55, 255, 255, 0)).strokeColor(Color.RED).strokeWidth(1));
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        mapView.onDestroy();
    }
    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        mapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        mapView.onPause();
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mapView.onSaveInstanceState(outState);
    }
}